package com.hrcx.mystery.controller;

import com.alibaba.fastjson.JSONObject;
import com.hrcx.mystery.common.CommonPage;
import com.hrcx.mystery.common.CommonResult;
import com.hrcx.mystery.common.PageParamRequest;
import com.hrcx.mystery.dto.MerchantProductsSearchDto;
import com.hrcx.mystery.dto.admin.NewMysteryDto;
import com.hrcx.mystery.dto.admin.PrizeMysteryDto;
import com.hrcx.mystery.dto.admin.UpdateMysteryDto;
import com.hrcx.mystery.exception.MysteryException;
import com.hrcx.mystery.pojo.EbMystery;
import com.hrcx.mystery.pojo.EbMysteryMerchandiseAssociated;
import com.hrcx.mystery.service.EbMysteryService;
import com.hrcx.mystery.vo.EbStoreProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <h1>盲盒管理员请求控制器</h1>
 *
 * @author LinXiPeng
 * @className MysteryAdminController
 * @date 2021/12/15 9:20
 * Version 1.0
 */
@RestController
@RequestMapping("api/mystery/admin")
@Api(tags = {"盲盒模块（后台）"})
@Slf4j
public class MysteryAdminController {
    @Resource
    private EbMysteryService ebMysteryService;

    /**
     * 创建盲盒
     */
    @ApiOperation("创建盲盒(同时添加奖品)")
    @RequestMapping(value = "creatMystery", method = RequestMethod.POST)
    public CommonResult<JSONObject> creatMystery(@Validated @RequestBody NewMysteryDto newMysteryDto) {
        return ebMysteryService.creatMystery(newMysteryDto) ? CommonResult.success("创建成功") : CommonResult.failed("创建失败");
    }

    /**
     * 添加奖品进入盲盒
     */
    @ApiOperation("向已有盲盒添加奖品")
    @RequestMapping(value = "insertProduct", method = RequestMethod.POST)
    public CommonResult<JSONObject> insertProduct(@Validated @RequestBody PrizeMysteryDto prizeMysteryDto) {
        return ebMysteryService.insertProduct(prizeMysteryDto.getAssociatedList(), prizeMysteryDto.getId()) ? CommonResult.success("添加成功") : CommonResult.failed("添加失败");
    }

    /**
     * 更新盲盒配置
     */
    @ApiOperation("更新盲盒配置")
    @RequestMapping(value = "updateMystery", method = RequestMethod.PUT)
    public CommonResult<JSONObject> updateMystery(@Validated @RequestBody UpdateMysteryDto updateMysteryDto) {
        return ebMysteryService.updateMystery(updateMysteryDto) ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }

    /**
     * 切换盲盒状态
     */
    @ApiOperation("切换盲盒状态")
    @RequestMapping(value = "toggleMysteryStatus", method = RequestMethod.PUT)
    public CommonResult<JSONObject> toggleMysteryStatus(@RequestParam Integer bid) {
        EbMystery ebMystery = ebMysteryService.lambdaQuery().eq(EbMystery::getId, bid)
                .select(EbMystery::getEndtime, EbMystery::getName).one();
        //如果原本的状态为false,则需要开启,但是前提是结束时间要比开启时间晚
        if (new Date().compareTo(ebMystery.getEndtime()) > 0) {
            throw new MysteryException(ebMystery.getName() + "开启失败,原因:结束时间比开启时间早,请先修改结束时间!");
        }
        return ebMysteryService.changeMysteryStatus(String.valueOf(bid)) ? CommonResult.success("切换成功") : CommonResult.failed("切换失败");
    }

    /**
     * 查询该商家的所有商品
     */
    @ApiOperation("查询该商家的所有商品")
    @RequestMapping(value = "getMerchantProduct", method = RequestMethod.GET)
    public CommonResult<CommonPage<EbStoreProductVo>> getMerchantProduct(@Validated MerchantProductsSearchDto merchantProductsSearchDto,
                                                                         @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(ebMysteryService.getMerchantProduct(merchantProductsSearchDto, pageParamRequest)));
    }

    /**
     * 更新奖品
     */
    @ApiOperation("更新奖品")
    @RequestMapping(value = "updatePrize", method = RequestMethod.PUT)
    public CommonResult<JSONObject> updatePrize(@RequestBody EbMysteryMerchandiseAssociated mysteryMerchandiseAssociated) {
        return ebMysteryService.updatePrize(mysteryMerchandiseAssociated) ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }
}